package com.black.web.app.store;

import com.black.app.domain.req.store.add.AppStoreCategoryAddReq;
import com.black.app.domain.req.store.query.AppStoreCategoryQueryReq;
import com.black.app.domain.resp.store.AppStoreCategoryResp;
import com.black.app.domain.resp.store.AppStoreCategoryTreeResp;
import com.black.app.service.store.AppStoreCategoryService;
import com.black.common.annotation.Log;
import com.black.common.core.controller.BaseController;
import com.black.common.core.domain.R;
import com.black.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "app-商品分类管理api")
@RestController
@RequestMapping(value = "/app/category")
public class AppStoreCategoryController extends BaseController {

    @Autowired
    private AppStoreCategoryService appStoreCategoryService;

    @ApiOperation(value = "获取商品分类树")
    @GetMapping(value = "/tree")
    public R<List<AppStoreCategoryTreeResp>> tree(){
        return R.ok(this.appStoreCategoryService.tree());
    }

    @ApiOperation(value = "查询商品分类列表")
    @GetMapping(value = "/list")
    public R<List<AppStoreCategoryResp>> list(AppStoreCategoryQueryReq req){
        List<AppStoreCategoryResp> resp = this.appStoreCategoryService.search();
        return R.ok(resp);
    }

    @ApiOperation(value = "根据分类id查询")
    @GetMapping(value = "/{id}")
    public R<AppStoreCategoryResp> findById(@PathVariable(value = "id") Long id){
        return R.ok(this.appStoreCategoryService.findById(id));
    }

    @Log(title = "商品分类管理", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增商品分类")
    @PostMapping
    public R add(@RequestBody @Valid AppStoreCategoryAddReq req){
        return isSuccess(this.appStoreCategoryService.add(req));
    }

    @Log(title = "商品分类管理", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除商品分类")
    @DeleteMapping(value = "/{id}")
    public R delete(@PathVariable(value = "id") Long id){
        return isSuccess(this.appStoreCategoryService.deleteById(id));
    }
}
